package com.cisco.pso;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.cisco.pso.FileItem;

public class FileListReader {

	protected List<FileItem> items = new ArrayList<FileItem>();

	public FileListReader(Reader reader) {

		BufferedReader br = new BufferedReader(reader);

		Set<String> keys = new HashSet<String>();
		
		while (true) {

			try {
				String line = br.readLine();
				if (line == null || line.trim().isEmpty())
					break;
				
				if(keys.add(line)==false)
					throw new RuntimeException("duplicate file name");

				FileItem item = new FileItem(line, getShortName(line));

				items.add(item);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}

		}

	}

	protected String getShortName(String fullName) {
		int index = fullName.lastIndexOf('/');
		return fullName.substring(index + 1);
	}

	public List<FileItem> getItems() {
		return items;
	}

	public void setItems(List<FileItem> items) {
		this.items = items;
	}

}
